package com.jxc.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jxc.dao.OrderListDao;
import com.jxc.entity.Orderlist;
import com.jxc.util.PageBean;

public class OrderListDaoImpl extends HibernateDaoSupport implements OrderListDao {

	public void deleteOrderList(Orderlist orderlist) {

		getHibernateTemplate().delete(orderlist);
	}

	public Orderlist getOrderList(Orderlist orderlist) {
		return (Orderlist)getHibernateTemplate().get(Orderlist.class, orderlist.getId()) ;
	}

	public PageBean getOrderListList(final String hql, final PageBean pb) {
		 getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				 Query query=session.createQuery(hql);
				 pb.setTotalitem(query.list().size());
				 query.setFirstResult((pb.getPageon()-1)*pb.getPagesize());
		         query.setMaxResults(pb.getPagesize());
		         pb.getTotalpage();
		         pb.setList(query.list());
				return pb;
			}
		});
		return pb;
	}

	public void saveOrderList(Orderlist orderlist) {

		getHibernateTemplate().save(orderlist);
	}

	public void upateOrderList(Orderlist orderlist) {
		getHibernateTemplate().update(orderlist);
	}

}
